//
//  XLQauthViewController.swift
//  微博
//
//  Created by macbook on 16/9/29.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit
import SVProgressHUD

class XLQauthViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view = webView
        view.backgroundColor = UIColor.whiteColor()
        
        setupUI()
    }
    
    //设置导航栏按钮
    func setupUI(){
    
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(XLQauthViewController.cancelClcik))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: UIBarButtonItemStyle.Plain, target: self, action:#selector(XLQauthViewController.autoFillClick))
    }
    
    //取消按钮的实现
    @objc private func cancelClcik(){
        
        dismissViewControllerAnimated(true, completion: nil)
        
    }
    //自动填充按钮的实现
    @objc private func autoFillClick(){
        
        print("点击了")
        
    }


    //懒加载webView
    private lazy var webView:UIWebView = {
    
        let webview = UIWebView()
        webview.delegate = self
        let url = NSURL(string: "https://api.weibo.com/oauth2/authorize?client_id=1387814882&redirect_uri=http://www.baidu.com")
        let request = NSURLRequest(URL: url!)
        webview.loadRequest(request)
        return webview
    }()
    

}


extension XLQauthViewController:UIWebViewDelegate{

    func webViewDidStartLoad(webView: UIWebView) {
       SVProgressHUD.show()
        
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        
        SVProgressHUD.dismiss()
    }
    
    //截取code
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        //ba url转换成字符串
        let urlString = request.URL?.absoluteString
        
        if let u = urlString where u.hasPrefix("http://www.baidu.com") {
           
            let qusest = request.URL?.query
//            print(qusest);
            //字符串截取code
            let code = qusest?.substringFromIndex("code=".endIndex)
            loadUserAccess(code!)
//            print(code)
            
        }
       print(request.URL?.absoluteString)
        return true
        
    }
}


extension XLQauthViewController{

    func loadUserAccess(code:String){
    
        XLNetwork.sharedTools.oautnLoadUserAccout(code, success: { (response) in
            //判断请求回来的response是否有值,是否可以转成字典
            guard let res = response as? [String:AnyObject] else
            {
                return
            }
            let userAccount = XLUserAccount(dict: res)
//            print("返回\(userAccount)")
            
            self.loadUserInfo(userAccount)
                        print(response)
            }) { (error) in
            print(error)
        }
    }
    
    //把token和uid传进去就可以获得个人信息
    func loadUserInfo(userAccount:XLUserAccount){
        
        XLNetwork.sharedTools.oauthUserInfo(userAccount, success: { (responst) in
            
            guard let res = responst as? [String:AnyObject] else{
            
                return
            }
            //因为他是任意类型所以要转换成字符串
            userAccount.avatar_large = res["avatar_large"] as? String
            userAccount.screen_name = res["screen_name"] as? String
            
            //用户名字和uid
//            print(userAccount)
            }) { (error) in
                print("失败\(error)")
        }
    
        
    }
    
    
    
    
    
    
}













